package com.controller;

import com.entity.Apply;
import com.entity.User;
import com.service.ApplyService;
import com.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.Date;

@Controller
public class ApplyController {

    @Autowired
    private ApplyService applyService;
    @Autowired
    private UserService userService;

    /**
     * 我的申请列表
     *
     * @param apply
     * @param session
     * @return
     */
    @RequestMapping("/userApplyList.do")
    public String userApplyList(Apply apply, HttpSession session) {
        User user = (User) session.getAttribute("user");
        apply.setUserid(user.getId());
        session.setAttribute("myApply", applyService.userApplyList(apply));
        return "/apply/userApplyList";
    }

    /**
     * 贷款申请表展示
     *
     * @param apply
     * @param session
     * @return
     */
    @RequestMapping("/applyInfo.do")
    public String applyInfo(Apply apply, HttpSession session) {
        User user1 = userService.userFindUser(apply.getUserid());
        session.setAttribute("user", user1);
        session.setAttribute("apply", applyService.applyInfo(apply));
        return "/apply/applyInfo";
    }

    /**
     * 贷款申请
     *
     * @param apply
     * @return
     */
    @RequestMapping("/userDoApply.do")
    public ModelAndView userDoApply(Apply apply) {
        Date date = new Date();
        java.sql.Date transDate = new java.sql.Date(date.getTime());
        apply.setApplydate(transDate);
        apply.setSignaturedate(transDate);
        applyService.userDoApply(apply);
        return new ModelAndView("redirect:/userApplyList.do");
    }
    /**
     * 用户 - 贷款申请列表 - 编辑
     *
     * @param apply
     * @return
     */
    @RequestMapping("/userEditApply.do")
    public String userEditApply(Apply apply,HttpSession session) {
        session.setAttribute("userApplyEdit", applyService.applyInfo(apply));
        return "/apply/userApplyEditor";
    }
    /**
     * 用户 - 贷款申请列表 - 编辑提交
     *
     * @param apply
     * @return
     */
    @RequestMapping("/userDoEditApply.do")
    public ModelAndView userDoEditApply(Apply apply) {
        applyService.userDoEditApply(apply);
        return new ModelAndView("redirect:/userApplyList.do");
    }
    /**
     * 用户 - 贷款列表页面再次申请按钮
     *
     * @param apply
     * @return
     */
    @RequestMapping("/userReDoApply.do")
    public ModelAndView userReDoApply(Apply apply) {
        applyService.userReDoApply(apply);
        return new ModelAndView("redirect:/userApplyList.do");
    }

    /**
     * 还款申请列表
     *
     * @param apply
     * @return
     */
    @RequestMapping("/userRepayList.do")
    public String userRepayList(Apply apply, HttpSession session) {
        User user = (User) session.getAttribute("user");
        apply.setUserid(user.getId());
        apply.setStatus(1);
        session.setAttribute("repayList", applyService.userRepayList(apply));
        return "/repayment/reList";
    }

    /**
     * 还款申请
     *
     * @param apply
     * @return
     */
    @RequestMapping("/userRepay")
    public String userRepay(Apply apply, HttpSession session) {
        apply.setId(apply.getId());
        Apply apply1 = applyService.applyInfo(apply);
        //放款日和还款日 相隔天数计算
        Date date = new Date();
        long beginTime = apply1.getDuedate().getTime();
        long endTime = date.getTime();
        long betweenDays = (long) ((endTime - beginTime) / (1000 * 60 * 60 * 24) + 0.5);
        //还款金额计算
        BigDecimal amount = new BigDecimal(String.valueOf(apply1.getAmount()));
        //公式  利息 = 贷款金额 * 天数 * 日利率
        BigDecimal repayamount = amount.multiply(BigDecimal.valueOf(betweenDays)).multiply(BigDecimal.valueOf(0.00014));
        //公式  还款总额 = 利息 + 贷款金额
        apply1.setRepayamount(repayamount.add(amount).setScale(2, BigDecimal.ROUND_HALF_UP));
        apply1.setApplyday(Math.toIntExact(betweenDays));
        session.setAttribute("repay", apply1);
        return "/repayment/userRepay";
    }

    /**
     * 还款记录
     *
     * @param apply
     * @return
     */
    @RequestMapping("/userRepayHistory")
    public String userRepayHistory(Apply apply, HttpSession session) {
        apply.setId(apply.getId());
        Apply apply1 = applyService.applyInfo(apply);
        //放款日和还款日 相隔天数计算
        Date date = new Date();
        long beginTime = apply1.getDuedate().getTime();
        long endTime = date.getTime();
        long betweenDays = (long) ((endTime - beginTime) / (1000 * 60 * 60 * 24) + 0.5);
        //还款金额计算
        BigDecimal amount = new BigDecimal(String.valueOf(apply1.getAmount()));
        //公式  利息 = 贷款金额 * 天数 * 日利率
        BigDecimal repayamount = amount.multiply(BigDecimal.valueOf(betweenDays)).multiply(BigDecimal.valueOf(0.00014));
        //公式  还款总额 = 利息 + 贷款金额
        apply1.setRepayamount(repayamount.add(amount).setScale(2, BigDecimal.ROUND_HALF_UP));
        apply1.setApplyday(Math.toIntExact(betweenDays));
        session.setAttribute("repay", apply1);
        return "/repayment/userRepayHistory";
    }

    /**
     * 还款申请列表
     *
     * @param apply
     * @return
     */
    @RequestMapping("/userRepay.do")
    public ModelAndView userRepay(Apply apply) {
        Date date = new Date();
        java.sql.Date transDate = new java.sql.Date(date.getTime());
        apply.setRepaydate(transDate);
        applyService.userRepay(apply);
        return new ModelAndView("redirect:/userRepayList.do");
    }

    /**
     * 删除申请
     *
     * @param apply
     * @return
     */
    @RequestMapping("/userDeleteApply.do")
    public ModelAndView userDeleteApply(Apply apply) {
        applyService.userDeleteApply(apply);
        return new ModelAndView("redirect:/userApplyList.do");
    }

    /**
     * 删除申请
     *
     * @param apply
     * @return
     */
    @RequestMapping("/adminDeleteApply.do")
    public ModelAndView adminDelApply(Apply apply) {
        applyService.userDeleteApply(apply);
        return new ModelAndView("redirect:/adminApplyList.do");
    }

    /**
     * 全部申请列表
     *
     * @param session
     * @return
     */
    @RequestMapping("/adminApplyList.do")
    public String adminApplyList(HttpSession session) {
        session.setAttribute("allApply", applyService.adminApplyList());
        return "/apply/adminApplyList";
    }

    /**
     * 管理员 - 贷款管理 - 发放贷款按钮
     *
     * @param apply
     * @return
     */
    @RequestMapping("/adminGrantApply.do")
    public ModelAndView adminGrantApply(Apply apply) {
        Date date = new Date();
        java.sql.Date transDate = new java.sql.Date(date.getTime());
        apply.setDuedate(transDate);
        applyService.adminGrantApply(apply);
        return new ModelAndView("redirect:/adminApplyList.do");
    }

    /**
     * 审核列表
     *
     * @param session
     * @return
     */
    @RequestMapping("/adminAuditApply.do")
    public String adminAuditApply(HttpSession session) {
        session.setAttribute("audits", applyService.adminAuditApply());
        return "/apply/adminAuditList";
    }

    /**
     * 申请审核
     *
     * @param apply
     * @return
     */
    @RequestMapping("/adminDoAuditApply.do")
    public ModelAndView adminDoAuditApply(Apply apply) {
        applyService.adminDoAuditApply(apply);
        return new ModelAndView("redirect:/adminAuditApply.do");
    }

    /**
     * 申请驳回
     *
     * @param apply
     * @return
     */
    @RequestMapping("/adminDoRejectApply.do")
    public ModelAndView adminDoRejectApply(Apply apply) {
        applyService.adminDoRejectApply(apply);
        return new ModelAndView("redirect:/adminAuditApply.do");
    }

    /**
     * 还款列表
     * @param session
     * @return
     */
    @RequestMapping("/adminRepayList.do")
    public String adminRepayList(HttpSession session) {
        session.setAttribute("rePayLists", applyService.adminRepayList());
        return "/apply/adminRepay";
    }
    /**
     * 用户 - 还款管理 - 确认收款
     *
     * @param apply
     * @return
     */
    @RequestMapping("/userConfirmReceiptApply.do")
    public ModelAndView userConfirmReceiptApply(Apply apply) {
        applyService.userConfirmReceiptApply(apply);
        return new ModelAndView("redirect:/userRepayList.do");
    }

    /**
     * 管理员 - 还款管理 - 确认收款
     *
     * @param apply
     * @return
     */
    @RequestMapping("/adminConfirmReceiptApply.do")
    public ModelAndView adminConfirmReceiptApply(Apply apply) {
        applyService.adminConfirmReceiptApply(apply);
        return new ModelAndView("redirect:/adminRepayList.do");
    }
}
